﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace drawing_print.util
{
    class Texts
    {
        /// <summary>
        /// txt转为PDF
        /// </summary>
        /// <param name="source"></param>
        /// <param name="output"></param>
        public static void textToPdf(string source, string output)
        {
           
            //第一个参数是txt文件物理路径
            string[] lines = System.IO.File.ReadAllLines(source, Encoding.GetEncoding("utf-8"));
            //iTextSharp.text.PageSize.A4    自定义页面大小
            Document doc = new Document(iTextSharp.text.PageSize.A4, 50, 20, 20, 20);
            PdfWriter pdfwriter =
                PdfWriter.GetInstance(doc, new FileStream(output,FileMode.Create));

            doc.Open();
           
            //创建我的基本字体
            BaseFont baseFT = BaseFont.CreateFont(Path.Combine(Application.StartupPath, "SIMHEI.TTF"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //创建字体      字体大小，字体粗細    字体颜色
            Font font = new Font(baseFT, 11, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

            Paragraph paragraph;
            foreach (string line in lines)
            {
                paragraph = new Paragraph(line, font);
                doc.Add(paragraph);
            }

            //关闭文件
            doc.Close();
        }

        /// <summary>
        /// 判断文件类型是否支持
        /// </summary>
        /// <param name="extension"></param>
        /// <returns></returns>
        public static bool isSupport(string extension)
        {
            switch (extension)
            {
                case ".txt":
                    return true;
                default:
                    return false;
            }
        }
    }
}
